package org.wyn.flowable.controller;


import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/process-definition")
public class ProcessDefinitionController {

    private static final Logger log = LoggerFactory.getLogger(ProcessDefinitionController.class);

    private RuntimeService runtimeService;

    private TaskService taskService;

    private RepositoryService repositoryService;

    private IdentityService identityService;


    @Autowired
    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    @Autowired
    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    @Autowired
    public void setRepositoryService(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @Autowired
    public void setIdentityService(IdentityService identityService) {
        this.identityService = identityService;
    }


    /**
     * 1.部署流程定义
     * @param bpmnXml
     * @return
     */
    @PostMapping("/deploy")
    @Transactional(rollbackFor = Exception.class)
    public String deploy(@RequestBody String bpmnXml) {
        Charset charset = StandardCharsets.UTF_8;
        try {
            bpmnXml = new String(Base64.getDecoder().decode(bpmnXml.getBytes(charset)), charset);
            bpmnXml = URLDecoder.decode(bpmnXml, charset.name());
        } catch (UnsupportedEncodingException e) {
            log.error("xml解码异常", e);
            return "xml解码异常，" + e.getMessage();
        }
        try {
            String resourceName = System.currentTimeMillis() + ".bpmn";
             repositoryService.createDeployment()
                    .addString(resourceName, bpmnXml)
                    .deploy();
        } catch (Exception e) {
            log.error("部署失败", e);
            return "部署失败，" + e.getMessage();
        }
        return "ok";
    }

    /**
     * 2.查询部署的流程定义
     * @param processDefinitionKey
     * @return
     */
    @GetMapping("/{processDefinitionKey}/bpmn")
    public String getBpmn(@PathVariable String processDefinitionKey) {
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .list();
        List<ProcessDefinition> pages = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .listPage(1, 30);
        return "ok";
    }
}
